<?php

namespace App\Admin\Controllers;

use App\Models\Layer;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class LayersController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '律师列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Layer());

        $grid->column('id', '编号')->sortable()->width(50);
        $grid->column('name', '名称')->text()->width(80);
        $grid->column('image', '头像')->image(env('APP_URL'), 50, 50)->width(100);
        $grid->column('domain', '擅长领域')->text()->width(200);
        $grid->column('content', '简介')->textarea();
        $grid->column('created_at', '创建时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });
        $grid->column('updated_at', '修改时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });

        $grid->disableRowSelector();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Layer::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Layer);
        $form->text('name','律师名称')->required();
        $form->text('domain','擅长领域')->help('可输入多个领域，使用; 隔开')->required();

        // 修改路径，去掉前导斜杠并使用storage_path
        $path = 'images/admin/layer';
        $form->image('image', '律师头像')->required()->move($path)->uniqueName()
            ->help('请上传正方形头像图片，建议尺寸200px × 200px，文件大小不超过2MB');

        $form->textarea('content', '简介')->required();
        $form->display('id', '编号');
        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        $form->tools(function ($tools){
            // 去掉`查看`按钮
            $tools->disableView();
        });

        return $form;
    }
}
